Raw Data

Summary Stats

Graphs

Column

Mean Lines (1)

Mean Lines (4)

Box Whisker Plot (1)

Box Whisker Plot (4)

R Help

_W_e_i_g_h_t _v_e_r_s_u_s _a_g_e _o_f _c_h_i_c_k_s _o_n _d_i_f_f_e_r_e_n_t _d_i_e_t_s

_D_e_s_c_r_i_p_t_i_o_n:

     The 'ChickWeight' data frame has 578 rows and 4 columns from an
     experiment on the effect of diet on early growth of chicks.

_U_s_a_g_e:

     ChickWeight
     
_F_o_r_m_a_t:

     An object of class 'c("nfnGroupedData", "nfGroupedData",
     "groupedData", "data.frame")' containing the following columns:

     weight a numeric vector giving the body weight of the chick (gm).

     Time a numeric vector giving the number of days since birth when
          the measurement was made.

     Chick an ordered factor with levels '18' < ... < '48' giving a
          unique identifier for the chick.  The ordering of the levels
          groups chicks on the same diet together and orders them
          according to their final weight (lightest to heaviest) within
          diet.

     Diet a factor with levels 1, ..., 4 indicating which experimental
          diet the chick received.

_D_e_t_a_i_l_s:

     The body weights of the chicks were measured at birth and every
     second day thereafter until day 20.  They were also measured on
     day 21.  There were four groups on chicks on different protein
     diets.

     This dataset was originally part of package 'nlme', and that has
     methods (including for '[', 'as.data.frame', 'plot' and 'print')
     for its grouped-data classes.

_S_o_u_r_c_e:

     Crowder, M. and Hand, D. (1990), _Analysis of Repeated Measures_,
     Chapman and Hall (example 5.3)

     Hand, D. and Crowder, M. (1996), _Practical Longitudinal Data
     Analysis_, Chapman and Hall (table A.2)

     Pinheiro, J. C. and Bates, D. M. (2000) _Mixed-effects Models in S
     and S-PLUS_, Springer.

_S_e_e _A_l_s_o:

     'SSlogis' for models fitted to this dataset.

_E_x_a_m_p_l_e_s:

     require(graphics)
     coplot(weight ~ Time | Chick, data = ChickWeight,
            type = "b", show.given = FALSE)
     
---
title: "Chick Weight Data"
output: 
  flexdashboard::flex_dashboard:
    orientation: columns
    vertical_layout: fill
    social: menu
    source_code: embed
---

```{r setup, include=FALSE}
# Install the latest packages via github
# devtools::install_github("rstudio/crosstalk")
# devtools::install_github("rstudio/DT")
# devtools::install_github("rstudio/flexdashboard")

library(flexdashboard)
library(tidyverse)
library(DT)
library(crosstalk)
library(knitr)
library(rmarkdown)
```

```{r CWPrep}
## Convert ChickWeight data into a tibble with desirable properties
CW <- ChickWeight %>%
  as_tibble() %>%
  mutate(Chick = as.numeric(as.character(Chick))) %>% 
  mutate(Diet = paste("Diet", as.character(Diet))) %>%
  rename(Weight = weight) %>%
  select(Chick, Diet, Time, Weight) %>%
  arrange(Chick, Diet, Time)

## Range for the slider App
rWgt <- range(CW$Weight)

# Summary Statistics by Diet and Time
CW_sum_stats <- CW %>% 
  group_by(Diet, Time) %>%
  summarise(N = n(),
            Mean = mean(Weight),
            SD = sd(Weight),
            Min = min(Weight),
            Median = median(Weight),
            Max = max(Weight))
```

Raw Data
=====================================

```{r rawData}
# Wrap data frame in SharedData
sdCW <- SharedData$new(CW)

# Use SharedData like a dataframe with Crosstalk-enabled widgets
bscols(widths = c(3, NA),
       list(
         filter_select("Chick", "Chick IDs", sdCW, ~Chick, multiple = TRUE), 
         filter_checkbox("Diet", "Diets", sdCW, ~Diet, columns = 2), 
         filter_select("Time", "Time (Days)", sdCW, ~Time, multiple = TRUE), 
         filter_slider("Weight", "Weight", sdCW, column=~Weight, width = "100%")
       ),
       datatable(sdCW, rownames = FALSE, 
            options = list(columnDefs = list(list(className = 'dt-center', targets = 0:1)), 
              pageLength = 15, deferRender=TRUE, scrollY="100%", scroller=TRUE))
)
```


Summary Stats
=====================================


```{r sumStats}
# Wrap data frame in SharedData
sdCWss <- SharedData$new(CW_sum_stats)

# Use SharedData like a dataframe with Crosstalk-enabled widgets
bscols(widths = c(3, NA),
       list(
         filter_checkbox("Diet", "Diets", sdCWss, ~Diet, columns = 2), 
         filter_select("Time", "Time (Days)", sdCWss, ~Time, multiple = TRUE)
       ),
       datatable(sdCWss, rownames = FALSE, 
            options = list(columnDefs = list(list(className = 'dt-center', targets = 0)), 
                           pageLength = 15, deferRender=TRUE, scrollY="100%", scroller=TRUE)) %>% 
         DT::formatRound(c('Mean', 'SD', 'Median'), digits = c(1, 2, 1))
)


```


   
Graphs
=====================================

Column  {.tabset .tabset-fade}
-------------------------------------

```{r basicPlot}
g <- ggplot(CW, aes(Time, Weight, colour = Diet)) +
  scale_x_continuous(breaks=unique(CW$Time)) +
  scale_y_continuous(breaks=seq(50, 350, by = 50)) +
  xlab("Time (days)") + 
  ylab("Weight (grams)") 
```


### Mean Lines (1)

```{r meanLines1}
g +
  geom_jitter(size = .4, width = 0.2) +
  stat_summary(fun.y="mean", geom="line", aes(group=Diet), size=1) +
  theme(legend.position = "bottom")
```

### Mean Lines (4)

```{r meanLines4}
g +
  facet_wrap(~Diet) +
  geom_jitter(size = .4, width = 0.2) +
  stat_summary(fun.y="mean", geom="line", aes(group=Diet), size=1) +
  theme(legend.position = "none")
```

### Box Whisker Plot (1)

```{r bwPlot1}
g +
  geom_boxplot(aes(group=interaction(Time, Diet))) +
  theme(legend.position = "bottom")
```

### Box Whisker Plot (4)

```{r bwPlot4}
g +
  facet_wrap(~Diet) +
  geom_boxplot(aes(group=interaction(Time, Diet))) +
  theme(legend.position = "none")
```

R Help
=====================================

```{r rHelp}
# This is not pretty but needs to be improved.
helpCW <- help("ChickWeight")
hcw <- tools:::Rd2txt(utils:::.getHelpFile(as.character(helpCW))) 
  cat(gsub("_", "", hcw, fixed = TRUE))
```